package com.web.base.core.service;

import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.base.core.dao.BankDao;
import com.web.base.core.exception.BankException;
import com.web.base.core.model.Bank;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 银行业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月19日
 */
@Service("coreBankService")
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class BankService
{
	@Autowired
	private BankDao bankDao;
	
	public BankService()
	{
	}
	
	public Bank save(Bank bank)
	{
		if (bankDao.exist(bank, "code"))
			throw new BankException("银行代码已经存在：" + bank.getCode());

		if (bank.getId() == null)
		{
			bankDao.save(bank);
		}
		else
		{
			bankDao.update(bank);
		}
		Bank a = bankDao.get(bank.getId());
		return a;
	}
	
	public Bank get(Integer id)
	{
		Bank bank = bankDao.get(id);
		return bank;
	}

	public Bank getByCode(String code)
	{
		Bank bank = bankDao.getObject("code", code);
		return bank;
	}
	
	public Page<Bank> list(Bank bank, Page<Bank> page)
	{
		page = bankDao.list(bank, page);
		return page;
	}
	
	public List<Bank> list(Bank bank)
	{
		List<Bank> list = bankDao.list(bank);
		return list;
	}
	
	public void delete(Bank bank)
	{
		bankDao.delete(bank);
	}
	
}
